## Chapter 6 (Downstream Effects) 
## Our Common Bonds 
## Winter 2021 - 2022  

## Read in source file 
source("ocb_replication_file.r") 

## Read in the AmeriSpeak Data 
friends <- read_sav(file="data/8423_Friendship_21Dec2018.sav") 

## Code up the treatment & perceptions of common ground 
friends <- friends %>% 
  mutate(treatment = ifelse(RND_01==1,1,0),
         common_ground = ifelse(Q12 < 6, -1*(Q12)+6,NA),
         party_agree = ifelse(Q13 < 6, -1*(Q13)+6,NA)) 
## can you combine the two items into an index? 
psych::alpha(cbind(friends$common_ground,
                   friends$party_agree)) 
## alpha = 0.64, so just analyze separately 

## Common Ground Graph 
pdf(file="figures/chi_levendusky_fig06001.pdf")
ggplot(data=friends) + 
  geom_bar(aes(x=common_ground, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("None at All","Not Too Much",
                              "Some","A Fair Amount",
                              "A Great Deal")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Treatment"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off()

## Look at values 
prop.table(table(friends$common_ground[friends$treatment == 0]))
prop.table(table(friends$common_ground[friends$treatment == 1]))
## control: 11% 50%, 27%, 8%, 2% 
## treatment: 8%, 47% 31% 11%, 4%

## Frequency of Agreeement 
pdf(file="figures/chi_levendusky_fig06002.pdf")
ggplot(data=friends) + 
  geom_bar(aes(x=party_agree, 
               y=100*..prop..,
               fill=as.factor(treatment)),
           position="dodge") + 
  xlab("") + 
  ylab("Percentage")  + 
  scale_x_continuous(breaks=c(1:5),
                     labels=c("Never","Not Too Often",
                              "Sometimes","Most of the Time",
                              "All the Time")) + 
  theme_bw() +   
  ggtitle("") + 
  scale_fill_grey(name="", 
                  labels=c("Control","Treatment"), 
                  start = 0.10, 
                  end = 0.8) + 
  theme(plot.title = element_text(hjust=0.5),
        legend.position = "bottom",
        panel.grid = element_blank()) 
dev.off()

## Look at the values 
prop.table(table(friends$party_agree[friends$treatment == 0]))
prop.table(table(friends$party_agree[friends$treatment == 1]))
## control: 8%, 65%, 23%, 3%, <1% 
## treatment: 6%, 61% 30%, 3%, <1% 

## Regressions 
m0 <- lm(common_ground ~ treatment, 
         data = friends)
m1 <- lm(party_agree ~ treatment, 
         data = friends)

stargazer(m0, m1, 
          covariate.labels = c("Treatment","Constant"),
          digits = 2, 
          keep.stat = c("n","rsq"),
          type = "html",
          out = "tables/raw output/appendix_table_six_one.html") 

## party cue data 
## raw issue positions & partisanship  
friends <- friends %>% 
  ## start with partisanship       
  mutate(partyid = ifelse(friends$Q1 == 1 & friends$Q1A ==1,1,
                          ifelse(friends$Q1 == 1 & friends$Q1A ==2,2,
                                 ifelse(friends$Q1 == 3 & friends$Q2 ==1,3,
                                        ifelse(friends$Q1 == 3 & (friends$Q2 != 1 & friends$Q2 != 2),4,
                                               ifelse(friends$Q1 == 3 & friends$Q2 == 2, 5,
                                                      ifelse(friends$Q1 == 2 & friends$Q1B ==2,6,
                                                             ifelse(friends$Q1 == 2 & friends$Q1B == 1,7,NA))))))),
         ## People who skip the initial PID question are then shown the leaner item, add them back in 
         partyid = ifelse(friends$Q1==98 & friends$Q2 == 1,3,partyid),
         partyid = ifelse(friends$Q1==98 & friends$Q2 == 2,5,partyid),
         partyid <- ifelse(friends$Q1==98 & friends$Q2 == 98,4,partyid), 
         ## issue positions 
         urban_growth = ifelse(friends$Q16<6,friends$Q16,NA),
         classaction = ifelse(friends$Q17<6,friends$Q17,NA), 
         ## take a position with your party (1-5 scale)
         party_growth = ifelse(partyid < 4, (-1*urban_growth) + 6, urban_growth),
         party_class = ifelse(partyid< 4, classaction,(-1*classaction)+6),
         ## adopt your party's position 
         sorted_growth = ifelse((partyid<4 & urban_growth < 3) | (partyid>4 & urban_growth > 3),1,0),
         sorted_growth = ifelse(partyid==4 | is.na(partyid) ,NA,sorted_growth),
         sorted_class = ifelse((partyid<4 & classaction > 3) | (partyid>4 & classaction < 3),1,0),
         sorted_class = ifelse(partyid==4 | is.na(partyid) ,NA,sorted_class))
cue_taking <- friends %>% 
  gather(sorted_class,sorted_growth,
         key="issue",value="position")
cue_alt <- friends %>% 
  gather(party_growth,party_class,
         key="issue",value="position")

## run the panel regression 
m0 <- lm_robust(position ~ treatment + as.factor(issue),
                data = cue_taking,
                clusters = CaseId, 
                se_type = "stata")
summary(m0) 
## see the expected 3 point drop in cue-taking 

############################
## Bovitz Forthright Data ##
############################

cues <- read_csv(file="data/bovitz_party_cue_data.csv")  

## Treatment Assignment & affective polarization measures 
cues <- cues %>% 
  ## friendship vs. control (1 = treatment, 0 = control) 
  mutate(friendship = ifelse(is.na(q24) & is.na(q23),1,0)) 

## subset to those who saw party cues (~1/3 of sample) 
cues <- cues %>% 
  filter(!is.na(q22)) 

## party ID 
cues <- cues %>% 
  mutate(party_id = ifelse(q37 == 1 & q19 == 1, 1,
                           ifelse(q37 == 1 & q19 == 2,2,
                                  ifelse(q37 == 2 & q21 == 1,3,
                                         ifelse(q37 == 2 & q21 == 2,5,
                                                ifelse(q37 == 3 & q20 == 2,6, 
                                                       ifelse(q37 == 3 & q20 == 1,7,NA)))))),
         party_id = ifelse(q37 == 2 & is.na(cues$q21),4,party_id)) 

## Cue Taking 
cues <- cues %>% 
  ## raw items for the DVs (original scales) 
  mutate(class_action = ifelse(!is.na(q126), q126,
                               ifelse(!is.na(q121),q121,
                                      ifelse(!is.na(q22),q22,NA))),
         mail_ballots = ifelse(!is.na(q128), q128,
                               ifelse(!is.na(q123),q123,
                                      ifelse(!is.na(q30),q30,NA))),
         energy = ifelse(!is.na(q129), q129,
                         ifelse(!is.na(q124),q124,
                                ifelse(!is.na(q31),q31,NA))),
         ## re-code so that higher values mean that you adopt your party's position 
         ca_party = ifelse(party_id < 4, class_action,
                           ifelse(party_id > 4, -1*(class_action)+6,NA)),
         mb_party = ifelse(party_id < 4, -1*(mail_ballots)+6,
                           ifelse(party_id > 4, mail_ballots, NA)),
         en_party = ifelse(party_id < 4, energy,
                           ifelse(party_id > 4, -1*(energy)+6,NA)),
         ## Did you adopt your party's position? 
         ca_sorted = ifelse(ca_party > 3,1,0),
         mb_sorted = ifelse(mb_party > 3,1,0),
         en_sorted = ifelse(en_party > 3,1,0)) 

## Stack Data for a Panel Regression 
panel <- cues %>% 
  gather(ca_sorted, mb_sorted, en_sorted,
         key="issue",value="position")

## model wtih issue fixed effects and standard errors clustered at the respondent level 
m4 <- lm_robust(position ~ friendship + as.factor(issue), 
                data = panel, 
                clusters = respondent_id, 
                se_type = "stata") 
summary(m4) 

## non-robust models 
m0a <- lm(position ~ treatment + as.factor(issue),
          data = cue_taking) 
m4a <- lm(position ~ friendship + as.factor(issue), 
          data = panel)  

## output regression results as a table 
stargazer(m0,m4, 
          se = starprep(m0a,m4a), 
          column.labels = c("AmeriSpeak","Bovitz"),
          covariate.labels = c("Treatment","Constant"),
          digits = 3, 
          keep.stat = c("n","rsq"),
          type = "html",
          out = "tables/raw output/table_six_one.html")

## Ambivalence Data 
cues <- cues %>% 
  mutate(other_pos_r = ifelse(q150 == 2,0,
                              ifelse(q151 == 4, 0.25, 
                                     ifelse(q151 == 3, 0.5,
                                            ifelse(q151 == 2, 0.75,
                                                   ifelse(q151 == 1,1,NA))))),
         other_neg_r = ifelse(q152 == 2,0,
                              ifelse(q153 == 4, 0.25, 
                                     ifelse(q153 == 3, 0.5,
                                            ifelse(q153 == 2, 0.75,
                                                   ifelse(q153 == 1,1,NA))))),
         same_pos_r = ifelse(q154 == 2,0,
                             ifelse(q155 == 4, 0.25, 
                                    ifelse(q155 == 3, 0.5,
                                           ifelse(q155 == 2, 0.75,
                                                  ifelse(q155 == 1,1,NA))))),
         same_neg_r = ifelse(q156 == 2,0,
                             ifelse(q157 == 4, 0.25, 
                                    ifelse(q157 == 3, 0.5,
                                           ifelse(q157 == 2, 0.75,
                                                  ifelse(q157 == 1,1,NA))))))
cues <- cues %>% 
  mutate(other_pos_d = ifelse(q51 == 2,0,
                              ifelse(q52 == 4, 0.25, 
                                     ifelse(q52 == 3, 0.5,
                                            ifelse(q52 == 2, 0.75,
                                                   ifelse(q52 == 1,1,NA))))),
         other_neg_d = ifelse(q53 == 2,0,
                              ifelse(q53 == 4, 0.25, 
                                     ifelse(q54 == 3, 0.5,
                                            ifelse(q54 == 2, 0.75,
                                                   ifelse(q54 == 1,1,NA))))),
         same_pos_d = ifelse(q55 == 2,0,
                             ifelse(q56 == 4, 0.25, 
                                    ifelse(q56 == 3, 0.5,
                                           ifelse(q56 == 2, 0.75,
                                                  ifelse(q156 == 1,1,NA))))),
         same_neg_d = ifelse(q57 == 2,0,
                             ifelse(q58 == 4, 0.25, 
                                    ifelse(q58 == 3, 0.5,
                                           ifelse(q58 == 2, 0.75,
                                                  ifelse(q58 == 1,1,NA))))))    
cues <- cues %>% 
  mutate(same_neg = ifelse(!is.na(same_neg_r),same_neg_r,
                           ifelse(!is.na(same_neg_d),same_neg_d,NA)),
         same_pos = ifelse(!is.na(same_pos_r),same_pos_r,
                           ifelse(!is.na(same_pos_d),same_pos_d,NA)),
         other_neg = ifelse(!is.na(other_neg_r),other_neg_r,
                            ifelse(!is.na(other_neg_d),other_neg_d,NA)),
         other_pos = ifelse(!is.na(other_pos_r),other_pos_r,
                            ifelse(!is.na(other_pos_d),other_pos_d,NA)),
         same_diff = same_pos - same_neg, 
         other_diff = other_pos - other_neg, 
         iden_consistent = same_pos + other_neg, 
         iden_inconsistent = same_neg + other_pos) 

## Run regressions to show these effects 
m0 <- lm(other_diff ~ friendship, 
         data = cues) 
m1 <- lm(other_pos ~ friendship, 
         data = cues) 
m2 <- lm(other_neg ~ friendship, 
         data = cues) 
m3 <- lm(same_diff ~ friendship, 
         data = cues) 
m4 <- lm(same_pos ~ friendship, 
         data = cues) 
m5 <- lm(same_neg ~ friendship, 
         data = cues) 

## output regression results as a table 
stargazer(m0, m1, m2, m3, m4, m5, 
          covariate.labels = c("Treatment","Constant"),
          digits = 3, 
          keep.stat = c("n","rsq"),
          type = "html",
          out = "tables/raw output/table_six_two.html")


